<?php

use yii\db\Schema;

class m140330_131336_init extends \yii\db\Migration {

    public function up() {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT = "分类"';
        }
        $this->createTable('category_type', [
            'id' => Schema::TYPE_PK,
            'code' => Schema::TYPE_STRING . '(20) NOT NULL COMMENT "代码"',
            'name' => Schema::TYPE_STRING . '(20)  NOT NULL COMMENT "名称"',
            'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 1 COMMENT "状态"',
            'KEY `code` (`code`)',
            'KEY `name` (`name`)',
            'KEY `status` (`status`)',
                ], $tableOptions);
        $this->createTable('category_category', [
            'id' => Schema::TYPE_PK,
            'type_id' => Schema::TYPE_INTEGER . '(20) NOT NULL  COMMENT "类别编号"',
            'code' => Schema::TYPE_STRING . '(20) NOT NULL COMMENT "代码"',
            'name' => Schema::TYPE_STRING . '(20)  NOT NULL COMMENT "名称"',
            'parent_id' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0  COMMENT "上级分类"',
            'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 1 COMMENT "状态"',
            'position' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 0 COMMENT "顺序"',
            'create_time' => Schema::TYPE_DATETIME . ' NOT NULL COMMENT "创建时间"',
            'update_time' => Schema::TYPE_DATETIME . ' NOT NULL COMMENT "更新时间"',
            'image_url' => Schema::TYPE_STRING . '(100) DEFAULT NULL COMMENT "图标"',
            'description' => Schema::TYPE_STRING . '(200) DEFAULT NULL  COMMENT "描述"',
            'KEY `type_id` (`type_id`)',
            'KEY `code` (`code`)',
            'KEY `parent_id` (`parent_id`)',
            'KEY `status` (`status`)',
            'KEY `position` (`position`)',
                ], $tableOptions);
        $this->addForeignKey('fk_type_id', 'category_category', 'type_id', 'category_type', 'id');
        return true;
    }

    public function down() {
        $this->dropTable('category_category');
        $this->dropTable('category_type');
        return true;
    }

}
